<%@ Application Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        Application["ResetMailRead"] = true;
        BLF.WWW_WWW.Wiki.StartupTools.Startup();
        // Code that runs on application startup
    }

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        if (BLF.WWW_WWW.Wiki.Pages.Instance == null)
        {
            BLF.WWW_WWW.Wiki.StartupTools.Startup();
        }
        // Code that runs on application startup
    }
    #region old
    /*
    protected void Application_EndRequest(Object sender, EventArgs e)
    {
        if ( HttpContext.Current.User != null && HttpContext.Current.User.Identity.Name != "")
        {
            //BLF.WWW_User.User.UpdateOnline(HttpContext.Current.User.Identity.Name);
            //update t_user set OnLine=0 where  LastLogin<dateadd(mi,-20,getdate());
            using (SqlConnection conn = new SqlConnection(IProvider.Config.WWW_User_UserConfig.DbConnStr))
            {
                string sql = string.Format(@"update t_user set OnLine=1,LastLogin=getdate(),LoginCount=LoginCount+1 where MemberNo='{0}';", HttpContext.Current.User.Identity.Name);
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
    }
 */
    #endregion
    protected void Application_EndRequest(Object sender, EventArgs e)
    {
        if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.Name != "")
        {            
            using (SqlConnection conn = new SqlConnection(IProvider.Config.WWW_User_UserConfig.DbConnStr))
            {               
                string sql = "update t_user set OnLine=1,LastLogin=getdate(),LoginCount=LoginCount+1 where MemberNo=@m;";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add(new SqlParameter("@m", HttpContext.Current.User.Identity.Name));
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        BLF.WWW_WWW.Wiki.SessionFacade.Breadcrumbs = new BLF.WWW_WWW.Wiki.BreadcrumbsManager();
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
    }
       
</script>
